/*******************************************************************************
Replication Materials for Blau, Kahn, Brummund, Cook, and Larson-Koester "Is 
There Still Son Preference in the United States?"

Table R5

Date Modified: 10/22/2019

*******************************************************************************/




*-------------------------------------------------------------------------------
                                   *LOAD DATA
*-------------------------------------------------------------------------------
use "$acs\acs_data", clear


*-------------------------------------------------------------------------------
                               *RESTRICT SAMPLE
*-------------------------------------------------------------------------------
keep if nonusbirth == 0 & father_sample != 1 & multi_sample != 1 & ///
	mult == 0 & female == 1 & oldc <=12 & inrange(age,18,40)==1
drop if citizen == 1 | marst == 5

*-------------------------------------------------------------------------------
                               *AGE AT FIRST BIRTH
*-------------------------------------------------------------------------------
egen max_age = rowmax(c_age*)
sum max_age // should be 12
gen birthage = age - max_age

*-------------------------------------------------------------------------------
                          *GENERATE CORE AND EXTENDED SAMPLES
*-------------------------------------------------------------------------------
gen extended = 1
gen core = 0
replace core = 1 if nonrkids == 0 & nonusbirth == 0 & sfrelate == 0 & ///
	father_sample != 1 & multi_sample != 1 & mult == 0 & female == 1 & /// 
	oldc <=12 & inrange(age,18,40)==1 & foster_hh == 0

*-------------------------------------------------------------------------------
                  *GENERATE SAMPLES, VARIABLES AND WEIGHTS FOR EACH PANEL
*-------------------------------------------------------------------------------
// Samples for each panel:
gen panelE = core == 1
gen panelF = extended == 1


// WEights for each panel:
gen wgtE = hhwtnorm
gen wgtF = perwtnorm

// Different dependetn variables:
forv count = 2/4{
	gen nc`count'plus=inrange(nchild,`count',.)==1
	label var nc2plus "`count' or More Children in HH"
} 

	
*-------------------------------------------------------------------------------
                                  *REGRESSIONS
*-------------------------------------------------------------------------------
label var chld1 "First Child Girl"	
local controls "lths scol cold" //leave out: hsdi
local controls "`controls' i.genrace i.region i.year age age2 age3" 

// REGRESSION LOOP
*note: this loops over each panel and each dependent variable, a seperate
*table is generated for each panel
foreach p in E F {

	// Main fertility results for the panel
		// NATIVE
		reg nchild chld1 birthage `controls' if native == 1 & panel`p' == 1 [aw=wgt`p'],  robust
		return list
		local b = round(r(table)[1, 1], 0.0001)
		sum nchild if native == 1 & panel`p' == 1  [aw=wgt`p']
		local mean = round(r(mean), 0.0001)
		local mean: display %5.4f `mean'
		local prct: display %5.4f 100*`b'/`mean'
		outreg2 using "$routput\Table_R15_Panel_`p'", replace excel dec(4) addtext(Dep. Var. Mean, `mean', Pct Effects, `prct') ctitle(nchild, Native) keep(chld1) label nocons nor2 ///
		title("Table T2 Cont With Age at 1st Birth Ctrl- Panel `p'")

		// IMMIGRANTS
		reg nchild chld1 birthage `controls' if native == 0 & panel`p' == 1 [aw=wgt`p'],  robust
		return list
		local b = round(r(table)[1, 1], 0.0001)
		sum nchild if native == 0 & panel`p' == 1  [aw=wgt`p']
		local mean = round(r(mean), 0.0001)
		local mean: display %5.4f `mean'
		local prct: display %5.4f 100*`b'/`mean'
		outreg2 using "$routput\Table_R15_Panel_`p'", append excel dec(4) addtext(Dep. Var. Mean, `mean', Pct Effects, `prct') ctitle(nchild, Immigrant) keep(chld1) label nocons nor2

		// BOTH
		reg nchild chld1 birthage `controls' if panel`p' == 1 [aw=wgt`p'],  robust
		return list
		local b = round(r(table)[1, 1], 0.0001)
		sum nchild if panel`p' == 1 [aw=wgt`p']
		local mean = round(r(mean), 0.0001)
		local mean: display %5.4f `mean'
		local prct: display %5.4f 100*`b'/`mean'
		outreg2 using "$routput\Table_R15_Panel_`p'", append excel dec(4) addtext(Dep. Var. Mean, `mean', Pct Effects, `prct') ctitle(nchild, Both) keep(chld1) label nocons nor2
	
	// Loop over other fertility variables
	foreach dep in nc2plus nc3plus nc4plus {
	
	
		// NATIVE
		reg `dep' chld1 birthage `controls' if native == 1 & panel`p' == 1 [aw=wgt`p'],  robust
		return list
		local b = round(r(table)[1, 1], 0.0001)
		sum `dep' if native == 1 & panel`p' == 1  [aw=wgt`p']
		local mean = round(r(mean), 0.0001)
		local mean: display %5.4f `mean'
		local prct: display %5.4f 100*`b'/`mean'
		outreg2 using "$routput\Table_R15_Panel_`p'", append excel dec(4) addtext(Dep. Var. Mean, `mean', Pct Effects, `prct') ctitle("`dep'", Native) keep(chld1) label nocons nor2

		// IMMIGRANTS
		reg `dep' chld1 birthage `controls' if native == 0 & panel`p' == 1 [aw=wgt`p'],  robust
		return list
		local b = round(r(table)[1, 1], 0.0001)
		sum `dep' if native == 0 & panel`p' == 1  [aw=wgt`p']
		local mean = round(r(mean), 0.0001)
		local mean: display %5.4f `mean'
		local prct: display %5.4f 100*`b'/`mean'
		outreg2 using "$routput\Table_R15_Panel_`p'", append excel dec(4) addtext(Dep. Var. Mean, `mean', Pct Effects, `prct') ctitle("`dep'", Immigrant) keep(chld1) label nocons nor2

		// BOTH
		reg `dep' chld1 birthage `controls' if panel`p' == 1 [aw=wgt`p'],  robust
		return list
		local b = round(r(table)[1, 1], 0.0001)
		sum `dep' if panel`p' == 1 [aw=wgt`p']
		local mean = round(r(mean), 0.0001)
		local mean: display %5.4f `mean'
		local prct: display %5.4f 100*`b'/`mean'
		outreg2 using "$routput\Table_R15_Panel_`p'", append excel dec(4) addtext(Dep. Var. Mean, `mean', Pct Effects, `prct') ctitle("`dep'", Both) keep(chld1) label nocons nor2
			
	
	}

}

